/*
 * RiskCauseBean.java
 * 
 * Copyright (c) 2011 iTrustSolution <hari(dot)christian(at)gmail(dot)com>. 
 * 
 * This file is part of RMS.
 * 
 * RMS is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * RMS is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with RMS.  If not, see <http ://www.gnu.org/licenses/>.
 */
package com.itrustsolution.rms.managedbean;

import com.icesoft.faces.component.ext.HtmlCommandButton;
import com.icesoft.faces.component.ext.HtmlInputText;
import com.icesoft.faces.component.ext.HtmlSelectOneMenu;
import com.icesoft.faces.component.ext.RowSelectorEvent;
import com.itrustsolution.rms.entities.RskRiskCause;
import com.itrustsolution.rms.entities.RskRiskCauseType;
import com.itrustsolution.rms.entities.TckTicket;
import com.itrustsolution.rms.util.HibernateUtil;
import com.itrustsolution.rms.util.JSFUtil;
import java.util.ArrayList;
import java.util.List;
import javax.faces.application.FacesMessage;
import javax.faces.model.SelectItem;

/**
 *
 * @author iTrustSolution <hari(dot)christian(at)gmail(dot)com>
 */
public class RiskCauseBean {
    private HtmlInputText inpTxtId;
    private HtmlInputText inpTxtDesc;
    private HtmlSelectOneMenu somCauseType;
    private HtmlSelectOneMenu somTicket;
    private HtmlCommandButton cmdBtnAdd;
    private HtmlCommandButton cmdBtnEdit;
    private HtmlCommandButton cmdBtnSave;
    private HtmlCommandButton cmdBtnDelete;    
    private HtmlCommandButton cmdBtnCancel;
    private RskRiskCause obj;
    private String result;
    // List of all rms client dept data
    private List<RskRiskCause> dataTables = new ArrayList<RskRiskCause>();
    // List of all cause type data
    private List<SelectItem> causeTypes;
    private String selectedCauseType;
    // List of all ticket data
    private List<SelectItem> tickets;
    private String selectedTicket;
    
    /** Creates a new instance of RiskCauseBean */
    public RiskCauseBean() {
    }
    
    public void add(){
        getInpTxtId().setValue(null);
        getInpTxtId().setSubmittedValue(null);
        getInpTxtDesc().setValue("");
        getInpTxtDesc().setSubmittedValue("");
        getSomCauseType().setValue(0);
        getSomTicket().setValue(0);
        getInpTxtDesc().setDisabled(false);
        getSomCauseType().setDisabled(false);
        getSomTicket().setDisabled(false);
        
        getCmdBtnAdd().setVisible(false);
        getCmdBtnEdit().setVisible(false);
        getCmdBtnSave().setVisible(true);
        getCmdBtnDelete().setVisible(false);
        getCmdBtnCancel().setVisible(true);
        
        getInpTxtDesc().setFocus(true);
    }
    
    public void edit(){
        getInpTxtDesc().setFocus(true);
        getInpTxtDesc().setDisabled(false);
        getSomTicket().setDisabled(false);
        
        getCmdBtnAdd().setVisible(false);
        getCmdBtnEdit().setVisible(false);
        getCmdBtnSave().setVisible(true);
        getCmdBtnDelete().setVisible(false);
        getCmdBtnCancel().setVisible(true);
    }
    
    public void save(){
        if(getInpTxtId().getValue() == null){
            obj = new RskRiskCause();
            result = " created";
        }else{
            obj = (RskRiskCause) HibernateUtil.getEntityById(RskRiskCause.class, Integer.parseInt((String) getInpTxtId().getValue()));
            result = " edited";
        }
        
        RskRiskCauseType causeType = (RskRiskCauseType) HibernateUtil.getEntityById(RskRiskCauseType.class, Integer.parseInt((String) getSomCauseType().getValue()));
        TckTicket ticket = (TckTicket) HibernateUtil.getEntityById(TckTicket.class, Integer.parseInt((String) getSomTicket().getValue()));
        
        obj.setRskRiskCauseType(causeType);
        obj.setTckTicket(ticket);
        obj.setRiskCauseDesc(inpTxtDesc.getValue().toString());
        HibernateUtil.saveOrUpdate(obj);
        
        JSFUtil.addFacesMessage(FacesMessage.SEVERITY_INFO, getInpTxtDesc().getValue() + result);
        
        cancel();
    }
    
    public void delete(){
        if (getInpTxtId().getValue() != null) {
            obj = (RskRiskCause) HibernateUtil.getEntityById(RskRiskCause.class, Integer.parseInt((String) getInpTxtId().getValue()));
            //obj.setRiskCauseIsactive(false);
            HibernateUtil.saveOrUpdate(obj);
            
            JSFUtil.addFacesMessage(FacesMessage.SEVERITY_INFO, getInpTxtDesc().getValue() + " deleted");
        }
    }
    
    public void cancel(){
        getInpTxtId().setValue(null);
        getInpTxtId().setSubmittedValue(null);
        getInpTxtDesc().setValue("");
        getInpTxtDesc().setSubmittedValue("");
        getSomCauseType().setValue(0);
        getSomTicket().setValue(0);
        
        getInpTxtDesc().setDisabled(true);
        getSomCauseType().setDisabled(true);
        getSomTicket().setDisabled(true);
        
        getCmdBtnAdd().setVisible(true);
        getCmdBtnEdit().setVisible(false);
        getCmdBtnSave().setVisible(false);
        getCmdBtnDelete().setVisible(false);
        getCmdBtnCancel().setVisible(false);
    }
    
    public void rowSelectionListener(RowSelectorEvent e) {
        if (!getCmdBtnSave().isVisible()) {
            // Display selected row
            obj = (RskRiskCause) getDataTables().get(e.getRow());
            getInpTxtId().setValue(String.valueOf(obj.getRiskCauseId()));
            getInpTxtId().setSubmittedValue(String.valueOf(obj.getRiskCauseId()));
            getInpTxtDesc().setValue(obj.getRiskCauseDesc());
            getInpTxtDesc().setSubmittedValue(obj.getRiskCauseDesc());
            getSomCauseType().setValue(obj.getRskRiskCauseType().getRiskCauseTypeId().toString());
            getSomCauseType().setSubmittedValue(obj.getRskRiskCauseType().getRiskCauseTypeId().toString());
            getSomTicket().setValue(obj.getTckTicket().getTicketId().toString());
            getSomTicket().setSubmittedValue(obj.getTckTicket().getTicketId().toString());
            
            // Show edit and remove button only when row selected
            getCmdBtnEdit().setVisible(true);
            getCmdBtnDelete().setVisible(true);
        }
    }
    
    /**
     * @return the inpTxtId
     */
    public HtmlInputText getInpTxtId() {
        return inpTxtId;
    }

    /**
     * @param inpTxtId the inpTxtId to set
     */
    public void setInpTxtId(HtmlInputText inpTxtId) {
        this.inpTxtId = inpTxtId;
    }
    
    /**
     * @return the inpTxtDesc
     */
    public HtmlInputText getInpTxtDesc() {
        return inpTxtDesc;
    }

    /**
     * @param inpTxtDesc the inpTxtDesc to set
     */
    public void setInpTxtDesc(HtmlInputText inpTxtDesc) {
        this.inpTxtDesc = inpTxtDesc;
    }
    
    /**
     * @return the cmdBtnAdd
     */
    public HtmlCommandButton getCmdBtnAdd() {
        return cmdBtnAdd;
    }

    /**
     * @param cmdBtnAdd the cmdBtnAdd to set
     */
    public void setCmdBtnAdd(HtmlCommandButton cmdBtnAdd) {
        this.cmdBtnAdd = cmdBtnAdd;
    }

    /**
     * @return the cmdBtnEdit
     */
    public HtmlCommandButton getCmdBtnEdit() {
        return cmdBtnEdit;
    }

    /**
     * @param cmdBtnEdit the cmdBtnEdit to set
     */
    public void setCmdBtnEdit(HtmlCommandButton cmdBtnEdit) {
        this.cmdBtnEdit = cmdBtnEdit;
    }

    /**
     * @return the cmdBtnSave
     */
    public HtmlCommandButton getCmdBtnSave() {
        return cmdBtnSave;
    }

    /**
     * @param cmdBtnSave the cmdBtnSave to set
     */
    public void setCmdBtnSave(HtmlCommandButton cmdBtnSave) {
        this.cmdBtnSave = cmdBtnSave;
    }

    /**
     * @return the cmdBtnDelete
     */
    public HtmlCommandButton getCmdBtnDelete() {
        return cmdBtnDelete;
    }

    /**
     * @param cmdBtnDelete the cmdBtnDelete to set
     */
    public void setCmdBtnDelete(HtmlCommandButton cmdBtnDelete) {
        this.cmdBtnDelete = cmdBtnDelete;
    }

    /**
     * @return the cmdBtnCancel
     */
    public HtmlCommandButton getCmdBtnCancel() {
        return cmdBtnCancel;
    }

    /**
     * @param cmdBtnCancel the cmdBtnCancel to set
     */
    public void setCmdBtnCancel(HtmlCommandButton cmdBtnCancel) {
        this.cmdBtnCancel = cmdBtnCancel;
    }
    
    /**
     * @return the dataTables
     */
    public List<RskRiskCause> getDataTables() {
        dataTables = (ArrayList<RskRiskCause>) HibernateUtil.createQuery("FROM RskRiskCause");
        return dataTables;
    }

    /**
     * @param dataTables the dataTables to set
     */
    public void setDataTables(List<RskRiskCause> dataTables) {
        this.dataTables = dataTables;
    }

    /**
     * @return the somCauseType
     */
    public HtmlSelectOneMenu getSomCauseType() {
        return somCauseType;
    }

    /**
     * @param somCauseType the somCauseType to set
     */
    public void setSomCauseType(HtmlSelectOneMenu somCauseType) {
        this.somCauseType = somCauseType;
    }

    /**
     * @return the somTicket
     */
    public HtmlSelectOneMenu getSomTicket() {
        return somTicket;
    }

    /**
     * @param somTicket the somTicket to set
     */
    public void setSomTicket(HtmlSelectOneMenu somTicket) {
        this.somTicket = somTicket;
    }

    /**
     * @return the causeTypes
     */
    public List<SelectItem> getCauseTypes() {
        List<RskRiskCauseType> list = (ArrayList<RskRiskCauseType>) HibernateUtil.createQuery("FROM RskRiskCauseType WHERE riskCauseTypeIsactive = 1");

        causeTypes = new ArrayList<SelectItem>();
        causeTypes.add(new SelectItem(null, "- Please Select -"));
        for (int i = 0; i < list.size(); i++) {
            causeTypes.add(new SelectItem(list.get(i).getRiskCauseTypeId().toString(), list.get(i).getRiskCauseTypeName()));
        }
        
        return causeTypes;
    }

    /**
     * @param causeTypes the causeTypes to set
     */
    public void setCauseTypes(List<SelectItem> causeTypes) {
        this.causeTypes = causeTypes;
    }

    /**
     * @return the tickets
     */
    public List<SelectItem> getTickets() {
        List<TckTicket> list = (ArrayList<TckTicket>) HibernateUtil.createQuery("FROM TckTicket");

        tickets = new ArrayList<SelectItem>();
        tickets.add(new SelectItem(null, "- Please Select -"));
        for (int i = 0; i < list.size(); i++) {
            tickets.add(new SelectItem(list.get(i).getTicketId().toString(), list.get(i).getTicketCode()));
        }
        
        return tickets;
    }

    /**
     * @param tickets the tickets to set
     */
    public void setTickets(List<SelectItem> tickets) {
        this.tickets = tickets;
    }

    /**
     * @return the selectedCauseType
     */
    public String getSelectedCauseType() {
        return selectedCauseType;
    }

    /**
     * @param selectedCauseType the selectedCauseType to set
     */
    public void setSelectedCauseType(String selectedCauseType) {
        this.selectedCauseType = selectedCauseType;
    }

    /**
     * @return the selectedTicket
     */
    public String getSelectedTicket() {
        return selectedTicket;
    }

    /**
     * @param selectedTicket the selectedTicket to set
     */
    public void setSelectedTicket(String selectedTicket) {
        this.selectedTicket = selectedTicket;
    }
}
